package com.sinosoft.hydra.um.helper;

import com.sinosoft.hydra.logging.LogModules;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/13.
 */
public class LoggerHelper {

    public static interface Property {
        public String toLogString();
    }

    public static class PropertyReader<T> implements Property {
        private List<T> source;
        private String property;

        public PropertyReader(List<T> source, String property) {
            this.source = source;
            this.property = property;
        }

        public String toLogString() {
            List<String> strings = new ArrayList<String>(source.size());
            for (T t : source) {
                try {
                    String s = BeanUtils.getProperty(t, property);
                    strings.add(StringUtils.isBlank(s) ? "NVL" : s);
                } catch (Exception e) {
                    strings.add("NVL");
                }
            }
            return StringUtils.join(strings, "，");
        }
    }

    public static class PropertyDes {
        private String property;
        private String description;

        public PropertyDes(String property, String description) {
            this.property = property;
            this.description = description;
        }

        public String getProperty() {
            return property;
        }

        public String getDescription() {
            return description;
        }
    }

    public static class PropertyMerge implements Property {
        private Object source;
        private Object target;
        private List<PropertyDes> propertyDeses = new ArrayList<PropertyDes>();

        public PropertyMerge(Object source, Object target) {
            this.source = source;
            this.target = target;
        }

        public PropertyMerge addPropertyDes(PropertyDes propertyDes){
            propertyDeses.add(propertyDes);
            return this;
        }

        public PropertyMerge addPropertyDes(String property, String description){
            propertyDeses.add(new PropertyDes(property, description));
            return this;
        }

        public String toLogString() {
            List<String> strings = new ArrayList<String>();
            for (PropertyDes propertyDes : propertyDeses) {
                try {
                    String de = BeanUtils.getProperty(source, propertyDes.getProperty()),
                            dp = BeanUtils.getProperty(target, propertyDes.getProperty());
                    de = StringUtils.isBlank(de) ? "NVL" : de;
                    dp = StringUtils.isBlank(dp) ? "NVL" : dp;
                    if (!de.equals(dp)) {
                        strings.add("[“" + de + "”修改为“" + dp + "”]");
                    }
                } catch (Exception e) {
                    strings.add("NVL");
                }
            }
            return StringUtils.join(strings, "，");
        }
    }

    private static List<Object> makeObjects(Object... args){
        List<Object> objs = new ArrayList<Object>();
        if (args.length > 0) {
            for (Object _obj : args) {
                if (_obj instanceof Property) {
                    objs.add(((Property) _obj).toLogString());
                } else if (_obj instanceof List) {
                    objs.add(StringUtils.join((List<String>)_obj, "，"));
                } else {
                    objs.add(_obj);
                }
            }
        }
        return objs;
    }

    public static void logInfo(String msg, Object... args) {
        List<Object> objs = makeObjects(args);
        LoggerFactory.getLogger(LogModules.TRACE_LOGGER).info(msg, objs.toArray(new Object[objs.size()]));
    }

    public static void logDebug(String msg, Object... args){
        List<Object> objs = makeObjects(args);
        LoggerFactory.getLogger(LogModules.TRACE_LOGGER).debug(msg, objs.toArray(new Object[objs.size()]));
    }

    public static void logError(String msg, Object... args){
        List<Object> objs = makeObjects(args);
        LoggerFactory.getLogger(LogModules.TRACE_LOGGER).error(msg, objs.toArray(new Object[objs.size()]));
    }

    public static void logWarn(String msg, Object... args){
        List<Object> objs = makeObjects(args);
        LoggerFactory.getLogger(LogModules.TRACE_LOGGER).warn(msg, objs.toArray(new Object[objs.size()]));
    }
}
